package com.zjdiepu.www.manage.api.file;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.biz.ibiz.IFileHelpService;
import com.zjdiepu.www.common.consts.ThreadFactory;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;

/**
 * 项目名称：dpqc-parent-manager-file <br/> 
 * 类名称：FileController <br/>  
 * 类描述： 文件操作ajax上传<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年4月10日 下午12:23:37  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年4月10日 下午12:23:37   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "FileController", produces = "application/json;charset=UTF-8")
public class FileController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IOtherAttachmentsService otherAttachmentsService;
	@Autowired
	private IFileHelpService fileHelpService;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: upload  <br/>
	 * @Description: 上传文件 <br/>
	 * @param file
	 * @param imageType
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "upload", method = {RequestMethod.POST} )
	public ApiResponse upload(@RequestParam("uploadfile") MultipartFile file, @RequestParam(defaultValue="0")Integer imageType, 
			@RequestParam(defaultValue="M_qt")String modeId,@RequestParam(defaultValue="false") boolean flag,@RequestParam(defaultValue="") String cusId) {
		ResultFlagType checkPath = fileHelpService.checkPath(file, imageType);
		if(!ResultFlagType.SUCCESS.equals(checkPath)) {
			return ApiResponse.fail(checkPath);
		}
		AbstractSysUser currentUser = getCurrentUser();
		String savePath = fileHelpService.getPathFromType(imageType);
		return fileHelpService.saveToFile(file, currentUser.getId(), currentUser.getName(), savePath, modeId,flag,cusId); 
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: uploadBatch  <br/>
	 * @Description:批量上传文件  <br/>
	 * @param multipartRequest
	 * @param imageType
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "uploadBatch", method = {RequestMethod.POST} )
	public ApiResponse uploadBatch(MultipartHttpServletRequest multipartRequest,
			@RequestParam(defaultValue="0")Integer imageType, @RequestParam(defaultValue="M_qt")String modeId,
			@RequestParam(defaultValue="false") boolean flag,@RequestParam(defaultValue="") String cusId) {
		ResultFlagType checkPath = fileHelpService.checkPath(multipartRequest, imageType);
		if(!ResultFlagType.SUCCESS.equals(checkPath)) {
			return ApiResponse.fail(checkPath);
		}
		AbstractSysUser currentUser = getCurrentUser();
		String savePath = fileHelpService.getPathFromType(imageType);
		Future<ApiResponse> future = ThreadFactory.THREAD_SERVICE.submit(new Callable<ApiResponse>() {
			@Override
			public ApiResponse call() {
				List<MultipartFile> files = new ArrayList<MultipartFile>();
				for (Iterator<String> it = multipartRequest.getFileNames(); it.hasNext();) {
					String fileName = it.next();
					MultipartFile file = multipartRequest.getFile(fileName);
					files.add(file);
				}
				return fileHelpService.saveBatchToFiles(files, currentUser.getId(), currentUser.getName(), savePath, modeId,flag,cusId); 
			}
		});
		try {
			return future.get();
		} catch (InterruptedException | ExecutionException e) {
			logger.error("upload files occur exception.", e);
			return ApiResponse.fail(ResultFlagType.FILE_UPLOAD_FAIL);
		}
	}
	
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: delete  <br/>
	 * @Description: 删除文件 <br/>
	 * @param fileKey
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value="delete", method = {RequestMethod.POST})
	public ApiResponse delete(String fileKey){
		return ApiResponse.success(true);
	}
}
